package com.ybc.chinese.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ybc.chinese.R;
import com.ybc.chinese.adapter.CollegeAdapter;
import com.ybc.chinese.bean.College;
import com.ybc.chinese.utils.CacheUtil;
import com.ybc.chinese.utils.CommonUtil;
import com.ybc.chinese.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * <p>高考<p/>
 * Created by Yubch on 17/2/18.
 */
public class CollegeFragment extends BaseFragment {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.gv_college)
    GridView gvCollege;
    @Bind(R.id.tv_time)
    TextView tvTime;

    private Context mCtx;
    private CollegeAdapter mAdapter;
    private static final String INDEX_COLLEGE = "index/college";

    public static CollegeFragment newInstance() {
        CollegeFragment collegeFragment = new CollegeFragment();
        return collegeFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mCtx = getActivity();
        View view = inflater.from(mCtx).inflate(R.layout.fragment_college, container, false);
        ButterKnife.bind(this, view);
        initView();
        List<College> colleges = getCache();
        if(!CommonUtil.isEmpty(colleges)) {
            mAdapter.setDatas(colleges, false);
        } else {
            showRunningDialog();
            request();
        }
        return view;
    }


    private void initView() {
        tvTitle.setText("2016高考试卷");
        mAdapter = new CollegeAdapter(mCtx);
        gvCollege.setAdapter(mAdapter);
        try {
            tvTime.setText("距离高考还有"+ DateUtil.getCollegeDays()+"天");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private List<College> getCache() {
        String jsonCollege = CacheUtil.getCache(mCtx, INDEX_COLLEGE);
        List<College> colleges = new ArrayList<>();
        if(!TextUtils.isEmpty(jsonCollege)) {
            colleges = new Gson().fromJson(jsonCollege, new TypeToken<List<College>>(){}.getType());
        }
        return colleges;
    }

    private void request() {
        if (!CommonUtil.isNetworkConnected(mCtx)) {
            dismissRunningDialog();
            CommonUtil.showToast(mCtx, R.string.no_net);
            return;
        }
        BmobQuery<College> query = new BmobQuery<>();
        query.order("createdAt");
        //执行查询，第一个参数为上下文，第二个参数为查找的回调
        query.findObjects(new FindListener<College>() {

            @Override
            public void done(List<College> list, BmobException e) {
                if (e == null) {
                    try {
                        mAdapter.setDatas(list, false);
                        CacheUtil.saveCache(mCtx, INDEX_COLLEGE, new Gson().toJson(list));
                    } catch (Exception ignore) {
                    }
                } else {
                    CommonUtil.showToast(mCtx, "数据请求失败");
                }
                dismissRunningDialog();
            }
        });
    }
}
